package com.jjc.MyWebApp.Validator;

import com.jfinal.core.Controller;
import com.jfinal.i18n.Res;
import com.jfinal.kit.StrKit;
import com.jjc.MyWebApp.Model.AjaxModel.JsonModel;
import com.jjc.MyWebApp.Model.User;
import com.jjc.MyWebApp.Plugin.ServicePlugin;
import com.jjc.MyWebApp.Service.UserService;


/**
 * Created by jiaji on 2017/5/11.
 */
public class LoginValidator extends BaseValidator {

    private UserService userService = (UserService) ServicePlugin.getService("userService");

    protected void validate(Controller c) {
        Res res = c.getAttr("_res");
        validateEmail("user.user_email", "Error", res.get("MyWebApp.User.ErrorMsg.EmailError"));
        validateString("passWord", 6, 12, "Error", res.get("MyWebApp.User.ErrorMsg.PwdError"));

        if (StrKit.notBlank(c.getPara("passWord_2"))){
            String email=c.getPara("user.user_email");
            if (userService.CacheGetUserByEmail(email) != null) {
                addError("Error", res.get("MyWebApp.User.ErrorMsg.EmailError_2"));
            }
            if (!StrKit.equals(c.getPara("passWord"),c.getPara("passWord_2"))){
                addError("Error", res.get("MyWebApp.User.ErrorMsg.PassDiffer"));
            }
        }

    }

    protected void handleError(Controller c) {
        c.renderJson(JsonModel.errorMsg(c.getAttrForStr("Error")));
    }
}
